package org.iabako.shared.entity.business;

import com.google.gwt.user.client.rpc.GwtTransient;
import org.hibernate.annotations.Index;
import org.iabako.shared.entity.enumeration.NotificationType;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.entity.user.User;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jose on 13/03/15.
 */

@Entity
@Table(name = "ENTERPRISE_NOTIFICATION")
public class Notification extends AbstractEntity {

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @Column(name = "TYPE", nullable = false)
    @Index(name = "TYPE")
    @Enumerated(EnumType.STRING)
    private NotificationType type;

    @Column(name = "CARDINAL")
    private int cardinal;

    @Column(name = "DISABLE")
    private boolean disable;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "USER_HIDDEN_NOTIFICATION", joinColumns = {
            @JoinColumn(name = "ID_NOTIFICATION", nullable = false)},
            inverseJoinColumns = {@JoinColumn(name = "ID_USER", nullable = false)})
    //To Avoid serializing exception in lazy/proxy objects.
    @GwtTransient
    private List<User> hiddenUsers;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "notification")
    //To Avoid serializing exception in lazy/proxy objects.
    @GwtTransient
    private List<NotificationEmail> notificationEmails;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_ENTERPRISE", nullable = false)
    @GwtTransient
    private Enterprise enterprise;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public NotificationType getType() {
        return type;
    }

    public void setType(NotificationType type) {
        this.type = type;
    }

    public int getCardinal() {
        return cardinal;
    }

    public void setCardinal(int cardinal) {
        this.cardinal = cardinal;
    }

    public boolean isDisable() {
        return disable;
    }

    public void setDisable(boolean disable) {
        this.disable = disable;
    }

    public List<User> getHiddenUsers() {
        if (hiddenUsers == null) {
            hiddenUsers = new ArrayList<User>();
        }
        return hiddenUsers;
    }

    public void setHiddenUsers(List<User> hiddenUsers) {
        this.hiddenUsers = hiddenUsers;
    }

    public List<NotificationEmail> getNotificationEmails() {
        return notificationEmails;
    }

    public void setNotificationEmails(List<NotificationEmail> notificationEmails) {
        this.notificationEmails = notificationEmails;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    @Override
    public void copyDatesAsString() {

    }
}